package cn.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
@Service
public class Tools {
	@Autowired
	WiselySettings wiselySettings;
	
	
	public static final String inputStream2String(InputStream in) throws UnsupportedEncodingException, IOException{  
        if(in == null)  
            return "";  
          
        StringBuffer out = new StringBuffer();  
        byte[] b = new byte[4096];  
        for (int n; (n = in.read(b)) != -1;) {  
            out.append(new String(b, 0, n, "UTF-8"));  
        }  
        return out.toString();  
    }  
	public static Map<String, Object> JsonToMap(String json) {
		Map<String, Object> map = null;
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			map = objectMapper.readValue(json, HashMap.class);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	public static List JsonTolist(String json) {
		List list =null;
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			 list = new ObjectMapper().readValue(json, ArrayList.class);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	  /**
			 * 将msg采用MD5算法处理,返回一个String结果
			 * @param msg 明文
			 * @return 密文
			 */
			public static String md5(String msg){
				try{
					MessageDigest md = 
						MessageDigest.getInstance("MD5");
					//原始信息input
					byte[] input = msg.getBytes();
					//加密信息output
					byte[] output = md.digest(input);//加密处理
					//采用Base64将加密内容output转成String字符串
					String s = Base64.encodeBase64String(output);
					return s;
				}catch(Exception ex){
					System.out.println("md5加密失败");
					return null;
				}
			}
			
	public String imgUp(MultipartFile files){
		SimpleDateFormat sdf = new SimpleDateFormat(
				"yyyyMMddHHmmss");// 可以方便地修改日期格式
		//上传图片
		String SiteLogo="";
		//上传的图片保存路径
		String filePath=wiselySettings.getPath()+"Ad/";
		File pathFile = new File(filePath);// 建文件夹
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		MultipartFile upFile = files;
		if(files.isEmpty()){
			SiteLogo="";
		}else{
			String newFileName = upFile.getOriginalFilename();//获取图片名称
			String newFilePath = filePath + newFileName;// 新路径
			File newFile = new File(newFilePath);
			newFileName = sdf.format(new Date()) + "_"
					+ upFile.getOriginalFilename();
			newFilePath = filePath + newFileName;
			newFile = new File(newFilePath);
			try {
				upFile.transferTo(newFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			SiteLogo="/upload/gym/Ad/"+newFileName;//保存到数据库的路径
		}
		return SiteLogo;
	}
	
	public  void delImg(String url){
		try {
			String pre=wiselySettings.getPath();
			if(!StringUtils.isEmpty(url)){
				String path=pre+url.replace("/upload/", "");
				File file=new File(path);
				if(file.exists()){
					file.delete();
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test(){
		String sq="D:/upload/";
		
	}
			
	
		public String reten() { 
		int a[] = new int[8];
		String aa ="";
		  for(int i=0;i<a.length;i++ ) {
		      a[i] = (int)(10*(Math.random()));
		      aa+=a[i];
		  }
		 return aa;
		}	
	
			
			
			
			
			
			
			
}
